MongoDB তে Query কনসেপ্ট

Java Technologies - জাভা মঙ্গোডিবি (Java MongoDB) - Query এবং Filter ব্যবহার
143

MongoDB একটি NoSQL ডাটাবেস, যেখানে ডেটা স্টোর করা হয় ডকুমেন্ট (Document) ফরম্যাটে (যেমন, JSON বা BSON)। MongoDB-তে ডেটা অনুসন্ধান এবং রিটার্ন করার জন্য querying ব্যবহৃত হয়। MongoDB এর query গুলি খুবই শক্তিশালী এবং নমনীয়, যা সহজেই বিভিন্ন ধরণের ডেটা ফিল্টারিং, আপডেট এবং অগ্রীগেট করতে সক্ষম।

MongoDB তে query করার জন্য MongoDB Java Driver ব্যবহার করা হয়, এবং এখানে MongoCollection ক্লাসের find(), insert(), update(), এবং delete() মেথডের মাধ্যমে ডেটাবেসের উপর অপারেশন করা হয়।

এখানে MongoDB Querying Concept এর মূল বিষয়গুলো আলোচনা করা হবে:


1. MongoDB Query Structure:

MongoDB তে একটি query সাধারণত find() মেথডের মাধ্যমে করা হয়। MongoDB query গুলি মূলত BSON (Binary JSON) ডকুমেন্ট হিসেবে থাকে। এটি SQL এর মতো নয়, বরং JSON মত কিউরি ব্যবহার করা হয়।

Basic MongoDB Query:

MongoCollection<Document> collection = database.getCollection("users");
Document query = new Document("name", "John");  // Searching for name = "John"
FindIterable<Document> result = collection.find(query);

এখানে:

  • database.getCollection("users"): "users" কালেকশনটি নির্বাচন করা হচ্ছে।
  • new Document("name", "John"): এটি একটি query condition যা "name": "John" এর সাথে মেলে এমন ডকুমেন্ট খুঁজবে।

2. Querying with Find():

MongoDB তে ডেটা অনুসন্ধান করার জন্য find() মেথড ব্যবহার করা হয়। find() মেথডটি MongoDB তে ডাটাবেস থেকে ডকুমেন্ট ফিল্টার করার জন্য ব্যবহার হয়।

Example:

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import com.mongodb.MongoClient;

public class MongoDBQueryExample {
    public static void main(String[] args) {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        MongoDatabase database = mongoClient.getDatabase("testdb");
        MongoCollection<Document> collection = database.getCollection("users");

        // Create a query to find documents with "name" equal to "John"
        Document query = new Document("name", "John");
        
        // Find matching documents
        for (Document doc : collection.find(query)) {
            System.out.println(doc.toJson());
        }
    }
}

ব্যাখ্যা:

  • collection.find(query) কিউরি দিয়ে name = John এর সাথে মেলে এমন ডকুমেন্টগুলি পাওয়া যাবে।

Output (যদি ডকুমেন্ট থাকে):

{"_id": ObjectId("..."), "name": "John", "age": 30, "city": "New York"}

3. Query with Conditions:

MongoDB তে query করার সময় আপনি একাধিক শর্ত বা কন্ডিশন ব্যবহার করতে পারেন। উদাহরণস্বরূপ, $gt, $lt, $eq ইত্যাদি অপারেটর ব্যবহার করে সংখ্যা, তারিখ ইত্যাদির উপর শর্ত প্রয়োগ করা যায়।

Example: Query with greater than condition ($gt)

Document query = new Document("age", new Document("$gt", 25));  // age > 25
FindIterable<Document> result = collection.find(query);

Other comparison operators:

  • $eq: সমান (Equal)
  • $gt: বড় (Greater than)
  • $gte: বড় অথবা সমান (Greater than or equal)
  • $lt: ছোট (Less than)
  • $lte: ছোট অথবা সমান (Less than or equal)
  • $ne: সমান নয় (Not equal)

Example with multiple conditions:

Document query = new Document("age", new Document("$gt", 25))
                           .append("city", "New York");  // age > 25 and city = "New York"

4. Logical Operators in MongoDB Queries:

MongoDB তে logical operators ব্যবহার করে একাধিক শর্ত একসাথে প্রয়োগ করা যেতে পারে। এগুলি হল $and, $or, $not, $nor ইত্যাদি।

Example:

Document query = new Document("$or", Arrays.asList(
    new Document("age", new Document("$lt", 30)),
    new Document("city", "New York")
));

এখানে, কিউরি দুটি শর্তের মধ্যে যেকোনো একটি পূর্ণ হলে ডকুমেন্ট রিটার্ন করবে:

  1. age < 30, অথবা
  2. city = New York

5. Projection in MongoDB Query:

MongoDB তে যখন একটি query run করা হয়, তখন আপনি শুধু প্রয়োজনীয় ফিল্ডগুলি (যেমন, নির্দিষ্ট কলাম) নির্বাচন করতে পারেন। এটি projection বলে এবং এটি find() মেথডে ব্যবহার করা হয়।

Example:

Document query = new Document("name", "John");
FindIterable<Document> result = collection.find(query)
                                           .projection(new Document("name", 1).append("age", 1));

এখানে:

  • new Document("name", 1): name ফিল্ডটি রিটার্ন করবে।
  • append("age", 1): age ফিল্ডটি রিটার্ন করবে।

Output:

{"_id": ObjectId("..."), "name": "John", "age": 30}

এখানে _id ফিল্ডটি ডিফল্টভাবে রিটার্ন হবে, যদি না তা বিশেষভাবে বাদ দেওয়া হয়।


6. Sorting and Limiting Results:

MongoDB তে query করার সময় আপনি sorting এবং limiting করতে পারেন। MongoDB sort() মেথড ব্যবহার করে ডেটা সাজানো এবং limit() মেথড ব্যবহার করে রিটার্ন করা ডকুমেন্টের সংখ্যা সীমাবদ্ধ করা যায়।

Example:

Document query = new Document();
FindIterable<Document> result = collection.find(query)
                                           .sort(new Document("age", 1))  // Sorting by age in ascending order
                                           .limit(5);  // Limit to 5 documents

এখানে:

  • sort(new Document("age", 1)): এটি ডেটাগুলিকে age এর ভিত্তিতে ascending অর্ডারে সাজাবে।
  • limit(5): এটি সর্বোচ্চ 5টি ডকুমেন্ট রিটার্ন করবে।

7. Aggregation in MongoDB:

MongoDB তে aggregation framework ব্যবহার করে আপনি জটিল কুয়েরি, গ্রুপিং, এবং অন্যান্য ফাংশনালিটি করতে পারেন। এটি অনেক শক্তিশালী এবং লজিক্যাল কুয়েরি তৈরিতে সহায়তা করে।

Example: Aggregation Pipeline

List<Bson> pipeline = Arrays.asList(
    Aggregates.match(Filters.gt("age", 25)),  // Filter documents where age > 25
    Aggregates.group("$city", Accumulators.avg("avgAge", "$age"))  // Group by city and calculate avg age
);
AggregateIterable<Document> result = collection.aggregate(pipeline);

এখানে:

  • Aggregates.match(): কন্ডিশন প্রদান করে।
  • Aggregates.group(): গ্রুপিং এবং অ্যাকিউমুলেটর (যেমন avg, sum, min, max) ব্যবহার করে।

MongoDB তে querying একটি গুরুত্বপূর্ণ প্রক্রিয়া, যা find(), update(), delete(), aggregation এবং অন্যান্য অপারেশনগুলোর মাধ্যমে ডেটাবেসে ডেটা ম্যানিপুলেট এবং অনুসন্ধান করতে ব্যবহৃত হয়। MongoDB-তে BSON ডকুমেন্ট ব্যবহার করা হয় এবং এর মাধ্যমে খুবই শক্তিশালী এবং নমনীয় কুয়েরি তৈরি করা সম্ভব। বিভিন্ন ধরনের শর্ত (comparison, logical operators), projection, sorting, limiting, এবং aggregation framework ব্যবহার করে MongoDB তে ডেটা দ্রুত এবং কার্যকরভাবে অনুসন্ধান ও ম্যানিপুলেট করা যায়।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...